//使用
// scale.switch_sl(num,system);/*num是是十进制值，system是想要转换进制的位数*/
function random_char(n) {
	//获取n位随机值
	var random_charset = [
		'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
		'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
	];
	var res = "";
	for (var i = 0; i < n; i++) {
		var id = Math.ceil(Math.random() * 35);
		res += random_charset[id];
	}
	return res;
}

function switch_sl(num, system) {
	// 十进制转其它进制，仅支持到十六进制
	var shiliu = [
			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
		],
		sl_arr = [];
	do {
		var fl_sl = Math.floor(num / system);

		var yu_sl = num % system;
		sl_arr.unshift(shiliu[yu_sl]);
		num = fl_sl;
	} while (num > 0)
	return sl_arr.join('');
}
// 字符串转16进制 str需要传入设备需要的utf-8（js字符串默认是Unicode编码utf16，需转成设备的utf8）解决中文问题,否则设备处理不了
function strToHexCharCode(str) {
	if (str === "")
		return "";
	var hexCharCode = [];
	// hexCharCode.push("0x");
	for (var i = 0; i < str.length; i++) {
		hexCharCode.push((str.charCodeAt(i)).toString(16));
	}
	return hexCharCode.join("");
}
// 16进制转字符串
async function hexCharCodeToStr(hex) {
	// //console.log(hex);
	var arr = hex.split(""),
		out = "",
		len = hex.length / 2;
	for (var i = 0; i < len; i++) {
		out += String.fromCharCode(parseInt(hex.substr(i * 2, 2), 16));
	}
	// //console.log(out);
	// 此时的out为UTF-8
	//蓝牙使用的是UTF-8编码
	// 解决乱码问题 （JS默认是Unicode编码的 也就是UTF-16）
	return await utf8to16(out)
	// var trimedStr = hex.trim();
	// var rawStr =
	// 	trimedStr.substr(0, 2).toLowerCase() === "0x" ?
	// 	trimedStr.substr(2) :
	// 	trimedStr;
	// var len = rawStr.length;
	// if (len % 2 !== 0) {
	// 	alert("Illegal Format ASCII Code!");
	// 	return "";
	// }
	// var curCharCode;
	// var resultStr = [];
	// for (var i = 0; i < len; i = i + 2) {
	// 	curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
	// 	resultStr.push(String.fromCharCode(curCharCode));
	// }
	// return resultStr.join("");
}
//十六进制字符串转字节数组
function Str2Bytes(str) {
	var pos = 0;
	var len = str.length;
	if (len % 2 != 0) {
		return null;
	}

	len /= 2;
	var hexA = new Array();
	for (var i = 0; i < len; i++) {
		var s = str.substr(pos, 2);
		var v = parseInt(s, 16);
		hexA.push(v);
		pos += 2;
	}
	return hexA;
}
//字节数组转十六进制字符串
function Bytes2Str(arr) {
	var str = "";
	for (var i = 0; i < arr.length; i++) {
		var tmp = arr[i].toString(16);
		if (tmp.length == 1) {
			tmp = "0" + tmp;
		}
		str += tmp;
	}
	return str;
}
//byte数组转字符串
function byteToString(arr) {
	if (typeof arr === 'string') {
		return arr;
	}
	var str = '',
		_arr = arr;
	for (var i = 0; i < _arr.length; i++) {
		var one = _arr[i].toString(2),
			v = one.match(/^1+?(?=0)/);
		if (v && one.length == 8) {
			var bytesLength = v[0].length;
			var store = _arr[i].toString(2).slice(7 - bytesLength);
			for (var st = 1; st < bytesLength; st++) {
				store += _arr[st + i].toString(2).slice(2);
			}
			str += String.fromCharCode(parseInt(store, 2));
			i += bytesLength - 1;
		} else {
			str += String.fromCharCode(_arr[i]);
		}
	}
	return str;
}
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function(bit) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('')
}
// 字符串转为ArrayBuffer对象，参数为字符串
function str2ab(str) {
	var buf = new ArrayBuffer(str.length*2); // 每个字符占用2个字节
	var bufView = new Uint16Array(buf);
	for (var i=0, strLen=str.length; i<strLen; i++) {
	bufView[i] = str.charCodeAt(i);
	}
	return buf;
}
function hex2auffer(hex){
	var typedArray = new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) {
	  return parseInt(h, 16)
	}))
	var buffer = typedArray.buffer
	return buffer
}
function utf8to16(str) {
	var out, i, len, c;
	var char2, char3;
	out = "";
	len = str.length;
	i = 0;
	while (i < len) {
		c = str.charCodeAt(i++);
		switch (c >> 4) {
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
				// 0xxxxxxx
				out += str.charAt(i - 1);
				break;
			case 12:
			case 13:
				// 110x xxxx 10xx xxxx
				char2 = str.charCodeAt(i++);
				out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
				break;
			case 14:
				// 1110 xxxx 10xx xxxx 10xx xxxx
				char2 = str.charCodeAt(i++);
				char3 = str.charCodeAt(i++);
				out += String.fromCharCode(((c & 0x0F) << 12) |
					((char2 & 0x3F) << 6) |
					((char3 & 0x3F) << 0));
				break;
		}
	}
	return out;
}

function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
    c = str.charCodeAt(i);
    if ((c >= 0x0001) && (c <= 0x007F)) {
    out += str.charAt(i);
    } else if (c > 0x07FF) {
    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
    out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
    } else {
    out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
    out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
    }
    }
    return out;
}
//字符串转byte数组
function textToArrayBuffer(s) {
	var i = s.length;
	var n = 0;
	var ba = new Array()
	for (var j = 0; j < i;) {
		var c = s.codePointAt(j);
		if (c < 128) {
			ba[n++] = c;
			j++;
		}
		else if ((c > 127) && (c < 2048)) {
			ba[n++] = (c >> 6) | 192;
			ba[n++] = (c & 63) | 128;
			j++;
		}
		else if ((c > 2047) && (c < 65536)) {
			ba[n++] = (c >> 12) | 224;
			ba[n++] = ((c >> 6) & 63) | 128;
			ba[n++] = (c & 63) | 128;
			j++;
		}
		else {
			ba[n++] = (c >> 18) | 240;
			ba[n++] = ((c >> 12) & 63) | 128;
			ba[n++] = ((c >> 6) & 63) | 128;
			ba[n++] = (c & 63) | 128;
			j+=2;
		}
	}
	return new Uint8Array(ba).buffer;
}

export default {
	switch_sl,
	strToHexCharCode,
	hexCharCodeToStr,
	Str2Bytes,
	Bytes2Str,
	byteToString,
	ab2hex,
	utf16to8,
	str2ab,
	hex2auffer,
	textToArrayBuffer
}
